package com.qingke.qixi.command_impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.qingke.qixi.App;
import com.qingke.qixi.Console;
import com.qingke.qixi.IDao.InvitationMapper;
import com.qingke.qixi.IDao.UserMapper;
import com.qingke.qixi.command.Command;
import com.qingke.qixi.pojo.Invitation;
import com.qingke.qixi.pojo.User;

public class InviteCommand extends Command {

	@Override
	public void execute() {
		User currentUser = App.getInstance().getCurrentUser();
		if (currentUser == null) {
			Console.println("您还未登录");
			return;
		}

		Console.println("【1】查看我的邀请信息");
		Console.println("【2】发送邀请");
		int num = Console.askInputInt("请输入要执行的命令序号");
		while (true) {
			if (num == 1 || num == 2) {
				break;
			} else {
				num = Console.askInputInt("序号不正确，请重新输入");
			}
		}

		if (num == 1) {
			checkMyInvitation(currentUser);
		} else {
			String targetGender = "M".equals(currentUser.getGender()) ? "female" : "male";
			List<User> users = getAllUsers(targetGender);
			printAllUsers(users);
			inviteUser(currentUser, users);
		}

	}

	private void checkMyInvitation(User currentUser) {

		SqlSession session = getSession();
		InvitationMapper invitationMapper = session.getMapper(InvitationMapper.class);
		List<Invitation> invitations = invitationMapper.selectMyInvitations(currentUser.getId());
		UserMapper userMapper = session.getMapper(UserMapper.class);
		int index = 1;
		for (Invitation invitation : invitations) {
			Console.print("【" + index + "】");
			Console.print("邀请人:" + userMapper.selectByPrimaryKey(invitation.getInviterUserId()).getName());
			Console.print("  邀请时间:" + invitation.getInviteTime());
			String accpet = "Y".equals(invitation.getIsAccepted()) ? "已接受" : "尚未接受";
			Console.println("  " + accpet);
		}
		String doit = Console.askInput("是否要对邀请进行接收或取消接受(Y/N)");
		
		while(true){
			if ("N".equalsIgnoreCase(doit)) {
				return;
			} else if ("Y".equalsIgnoreCase(doit)) {
				acceptInvitation(invitations);
			}else {
				doit = Console.askInput("别瞎G8乱按(Y/N)");
				continue;
			}
			break;
		}
	}

	private void acceptInvitation(List<Invitation> invitations) {
		SqlSession session = getSession();
		InvitationMapper invitationMapper = session.getMapper(InvitationMapper.class);
		int index = Console.askInputInt("请输入要操作的受邀记录编号") - 1;
		Invitation invitation = null;
		while(true){
			try {
				invitation = invitations.get(index);
				break;
			} catch (Exception e) {
				index = Console.askInputInt("您输入的受邀记录编号不存在，请重新输入") - 1;
			}
		}
		
		String accept = invitation.getIsAccepted();
		if("Y".equals(accept)){
			while(true){
				String ok = Console.askInput("该受邀记录您已经接受，是否要取消它(Y/N)?");
				if("Y".equalsIgnoreCase(ok)){
					invitation.setIsAccepted("N");
					invitationMapper.updateByPrimaryKeySelective(invitation);
					session.commit();
					Console.println("取消成功，已经向对方发送了一张好人卡");
				}else if("N".equalsIgnoreCase(ok)) {
					Console.println("你瞅瞅你，瞎G8乱按");
				}else {
					ok = Console.askInput("是否要取消该记录(Y/N)，别瞎G8乱按");
					continue;
				}
				break;
			}
		}else{
			while(true){
				String ok = Console.askInput("您尚未接受该邀请，是否要接受它(Y/N)?");
				if("Y".equalsIgnoreCase(ok)){
					invitation.setIsAccepted("Y");
					invitationMapper.updateByPrimaryKeySelective(invitation);
					session.commit();
					Console.println("接受成功，在那之后干了个爽");
				}else if("N".equalsIgnoreCase(ok)) {
					Console.println("你瞅瞅你，瞎G8乱按");
				}else {
					ok = Console.askInput("到底约不约(Y/N)，别瞎G8乱按");
					continue;
				}
				break;
			}
		}
		
		String doit = Console.askInput("是否要继续操作(Y/N)");
		while(true){
			if ("N".equalsIgnoreCase(doit)) {
				return;
			} else if ("Y".equalsIgnoreCase(doit)) {
				acceptInvitation(invitations);
			}else {
				doit = Console.askInput("别瞎G8乱按(Y/N)");
				continue;
			}
		}
	}

	private void inviteUser(User currentUser, List<User> users) {
		User invitee = null;
		int index = Console.askInputInt("请输入要邀请的用户编号") - 1;
		while (true) {
			try {
				invitee = users.get(index);
				break;
			} catch (Exception e) {
				index = Console.askInputInt("您输入的用户编号不存在，请重新输入") - 1;
			}
		}
		Invitation invitation = new Invitation();
		invitation.setInviteeUserId(invitee.getId());
		invitation.setInviterUserId(App.getInstance().getCurrentUser().getId());
		invitation.setInviteTime(new Date());
		invitation.setIsAccepted("N");
		SqlSession session = getSession();
		InvitationMapper invitationMapper = session.getMapper(InvitationMapper.class);
		invitationMapper.insert(invitation);
		session.commit();
		Console.println("邀请信息已发送");
		String doit = Console.askInput("还要继续邀请吗(Y/N)");
		if ("N".equalsIgnoreCase(doit)) {
			return;
		} else if ("Y".equalsIgnoreCase(doit)) {
			inviteUser(currentUser, users);
		}
		
		
	}

	private void printAllUsers(List<User> users) {
		Console.println("以下是可以邀请的所有用户");
		int i = 1;
		for (User user : users) {
			Console.print("【" + i + "】");
			Console.println(user);
			i++;
		}
	}

	private List<User> getAllUsers(String targetGender) {
		SqlSession session = getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		List<User> users;
		if ("male".equals(targetGender)) {
			users = userMapper.selectAllMale();
		} else {
			users = userMapper.selectAllFemale();
		}
		return users;
	}
}
